<template lang="pug">
Dialog.scan-login-dialog(title='微信扫码登录' height='400px' width='400px' :show='!!url' @close='url = ""' :fullscreen='false' :close-on-click-modal='false')
  img.w250.margin-auto(:src='url')
</template>

<script>
import $API from '@/api/services'
import { getQrcodeUrl, getWXScanLoginUrl } from '@/api/'
import qs from 'query-string'
export default {
  name: 'ScanLogin',
  components: {
    Dialog: _ => import('../base/dialog/'),
  },
  data () {
    return {
      url: '',
    }
  },
  methods: {
    open () {
      this.url = getQrcodeUrl(getWXScanLoginUrl())
      this.loopGetStatus()
    },
    loopGetStatus () {
      $API('getWXScanLoginStatus', []).then(({data}) => {
        if (data) {
          // console.log(data)
          this.$AUTH.setToken(qs.stringify(data))
          this.$vgo.tip('登录成功!', 'success')
          // 获取用户信息
          this.$store.dispatch('getUserInfo')
          this.url = ''
          // 845定制
          if ($globalconfig.MODULES.IS_LANJIUKJ_PROGRAM_OPEN) {
            setTimeout(() => {
              window.history.go(0)
            }, 200)
          }
        } else if (!data && this.url) {
          this.loopGetStatus()
        }
      })
    },
  },
}
</script>
